let express = require("express");

let app = express();

let router = express.Router();

let fs = require("fs");

//加载ejs模板引擎
let ejs = require("ejs");
const { stringify } = require("querystring");
const { json } = require("body-parser");

router.use(express.static(__dirname + "/static"));

router.get("/register", (req, res) => {
    res.render("register.html")
})

router.post("/register", (req, res) => {
    let persons = fs.readFileSync(__dirname + "/persons.json", "utf8");
    persons = JSON.parse(persons);
    let { tel, pass } = req.body;
    let users = persons.find((item) => {
        //console.log(item);
        return tel == item.username
    })
    
    let data = {tel,pass};
    if (users) {
        res.send("用户已存在");
    } else {
        persons.push(data);
        persons = JSON.stringify(persons);
        fs.writeFileSync(__dirname + "/persons.json", persons, { flag: "w" })
        res.send("注册成功");
    }
})

module.exports = router;